package com.thinkgem.jeesite.modules.ta.util;

import java.io.File; 

import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 

import org.w3c.dom.Document; 
import org.w3c.dom.NodeList;

public class GetWebServicePathUtil {

	public static String getPath(String userId,String fName,HttpServletRequest request) {
		String head = "";
		double rand = Math.random();
		String path = request.getSession().getServletContext().getRealPath("/WebServerPath.xml");
		try {
			File f = new File(path); 
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
			DocumentBuilder builder = factory.newDocumentBuilder(); 
			Document doc = builder.parse(f); 
			head = doc.getElementsByTagName("head").item(0).getFirstChild().getNodeValue();
			head += "?readonly=false&FormName=";
			NodeList nl = doc.getElementsByTagName("path"); 
			for (int i = 0; i < nl.getLength(); i++) { 
				String name = doc.getElementsByTagName("name").item(i).getFirstChild().getNodeValue();
				if(name.equals(fName)){
					head += doc.getElementsByTagName("ServerPath").item(i).getFirstChild().getNodeValue();
				}
			} 
			if(fName.equals("shoujianxiang")){
				head += "&type=1";
			}
			if(fName.equals("jieyueshenpi")){
				head += "&flag=0";
			}
			head += "&userid="+userId+"&ran="+rand;
			System.out.println(head);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return head;
    }  
	public static String getWebPath(HttpServletRequest request) {
		String head = "";
		String path = request.getSession().getServletContext().getRealPath("/WebServerPath.xml");
		try {
			File f = new File(path); 
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
			DocumentBuilder builder = factory.newDocumentBuilder(); 
			Document doc = builder.parse(f); 
			head = doc.getElementsByTagName("webServerPath").item(0).getFirstChild().getNodeValue();
			System.out.println(head);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return head;
    }  
	public static String getAttPath(HttpServletRequest request) {
		String head = "";
		String path = request.getSession().getServletContext().getRealPath("/WebServerPath.xml");
		try {
			File f = new File(path); 
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
			DocumentBuilder builder = factory.newDocumentBuilder(); 
			Document doc = builder.parse(f); 
			head = doc.getElementsByTagName("attServerPath").item(0).getFirstChild().getNodeValue();
			System.out.println(head);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return head;
    }  
	
	public static String getTpPath(String ItemId,String iid,String step,String userId,HttpServletRequest request) {
		String head = "";
		String path = request.getSession().getServletContext().getRealPath("/WebServerPath.xml");
		try {
			File f = new File(path); 
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
			DocumentBuilder builder = factory.newDocumentBuilder(); 
			Document doc = builder.parse(f); 
			head = doc.getElementsByTagName("tp").item(0).getFirstChild().getNodeValue();
			head += "?ItemId="+ItemId+"&iid="+iid+"&step="+step+"&userId="+userId;
			System.out.println(head);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return head;
    }  
	public static String mingxi(String iid,HttpServletRequest request) {
		String head = "";
		String path = request.getSession().getServletContext().getRealPath("/WebServerPath.xml");
		try {
			File f = new File(path); 
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
			DocumentBuilder builder = factory.newDocumentBuilder(); 
			Document doc = builder.parse(f); 
			head = doc.getElementsByTagName("mingxi").item(0).getFirstChild().getNodeValue();
			head +="?iid="+iid;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return head;
    }  
	public static String anjian(String iid,HttpServletRequest request) {
		String head = "";
		String path = request.getSession().getServletContext().getRealPath("/WebServerPath.xml");
		try {
			File f = new File(path); 
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
			DocumentBuilder builder = factory.newDocumentBuilder(); 
			Document doc = builder.parse(f); 
			head = doc.getElementsByTagName("anjian").item(0).getFirstChild().getNodeValue();
			head +="?iid="+iid;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return head;
    }  
}
